<?php

if(!isset($_SESSION)) session_start();
include("./API/BookClass.php");
include("./API/APIObject.php");
include("./API/Code/Filter.php");

$fieldNames = array(
    0 => "contactDetails_email",
    1 => "contactDetails_confirmEmail",
    2 => "contactDetails_phoneNumber",
    3 => "creditCardInformation_cardType",
    4 => "creditCardInformation_cardNumber",
    5 => "cerditCardInformation_cardHolderFirstName",
    6 => "creditCardInformation_cardHolderLastName",
    7 => "creditCardInformation_expirationDate_month",
    8 => "creditCardInformation_expirationDate_year",
    9 => "creditCardInformation_securityCode",
    10 => "billingAdress_country",
    11 => "billingAddress_streetAddres_part1",
    12 => "billingAddress_streetAddres_part2",
    13 => "billingAddress_city",
    14 => "billingAddress_zipCode",
    15 => "billingAddress_state",
    16 => "termsAndConditions",
    17 => "book",
    18 => "supplierType",
    19 => "roomRateKey",
    20 => "roomRateCode",
    21 => "roomTypeCode",
    22 => "hotelId"
);

$sessionVars = array(
    0 => "roomsNumber",
    1 => "checkIn",
    2 => "checkOut",
);

//Checking if all the data has been sent
$ok = true;

for ($i = 0; $i < 3; $i++) {
    if (!isset($_SESSION[$sessionVars[$i]]))
        $ok = false;
}


if ($ok) {
    for ($i = 1; $i <= $_SESSION["roomsNumber"]; $i++) {
        if (!isset($_SESSION["roomsData"][$i]["adults"]))
            $ok = false;

        if (!isset($_SESSION["roomsData"][$i]["childrenNr"]))
            $ok = false;

        if (!isset($_SESSION["roomsData"][$i]["childrenAge"]))
            $ok = false;

        if (!isset($_POST["rooms" . $i . "_smokingPreferenceId"]))
            $ok = false;


        if (!isset($_POST["guest_firstName" . $i]))
            $ok = false;

        if (!isset($_POST["guest_lastName" . $i]))
            $ok = false;
    }
}


for ($i = 0; $i <= 21; $i++) {
    if (!isset($_POST[$fieldNames[$i]])) {
        $ok = false;
    }
}


print_r($_POST);
echo "<br>";
print_r($_SESSION);

//Checking if all the data has been sent

if (!$ok) {
    //include("error_booking.php");
    echo "Error.... Not all data has been sent";
} else {

    $api = new APIObject();

// Get Room Availability
    $roomGroup1 = unserialize($_SESSION["roomGroup"]);
    $roomGroup2 = new RoomGroup();
    $roomGroup2->Room = $roomGroup1->Room;


    $roomAvail = $api->GetRoomAvailability
            (
            $_POST["hotelId"], $_SESSION["checkIn"], $_SESSION["checkOut"], NULL, $_POST["supplierType"], $_POST["roomRateKey"], $_POST["roomRateCode"], $_POST["roomTypeCode"], TRUE, TRUE, "HOTEL_DETAILS,ROOM_TYPES,ROOM_AMENITIES,PROPERTY_AMENITIES,HOTEL_IMAGES", $roomGroup2
    );

    //print_r($roomAvail);


    $book = new Book("55505", "cbrzfta369qwyrm9t5b8y8kf", $_SERVER['REMOTE_ADDR'], "", "15", "en_US", "en_EN", "USD");

    $book->SetGeneralData
            (
            $roomAvail->hotelId, $roomAvail->arrivalDate, $roomAvail->departureDate, $roomAvail->HotelRoomResponse->supplierType, $roomAvail->rateKey, $roomAvail->HotelRoomResponse->RoomType->{"@roomCode"}, $roomAvail->HotelRoomResponse->rateCode, $roomAvail->HotelRoomResponse->RateInfos->RateInfo->ChargeableRateInfo->{"@total"}
    );



    for ($i = 1; $i <= $_SESSION["roomsNumber"]; $i++) {
        $roomsData[$i - 1]['numberOfAdults'] = $_SESSION["roomsData"][$i]["adults"];
        $roomsData[$i - 1]['numberOfChildren'] = $_SESSION["roomsData"][$i]["childrenNr"];
        $roomsData[$i - 1]['childAges'] = $_SESSION["roomsData"][$i]["childrenAge"];
        $roomsData[$i - 1]['firstName'] = $_POST["guest_firstName" . $i];
        $roomsData[$i - 1]['lastName'] = $_POST["guest_lastName" . $i];
        $roomsData[$i - 1]['smokingPreference'] = $_POST["rooms" . $i . "_smokingPreferenceId"];
    }
    $book->SetGuestsData($_SESSION["roomsNumber"], $roomsData);

    $reservationInfo['email'] = $_POST["contactDetails_email"]; //"pufandreipetru@gmail.com";
    $reservationInfo['firstName'] = $_POST["cerditCardInformation_cardHolderFirstName"]; //"Test Booking";
    $reservationInfo['lastName'] = $_POST["creditCardInformation_cardHolderLastName"]; //"Test Booking";
    $reservationInfo['homePhone'] = $_POST["contactDetails_phoneNumber"]; //"0761256804";
    $reservationInfo['workPhone'] = $_POST["contactDetails_phoneNumber"]; //"0761256804";
    $reservationInfo['creditCardType'] = $_POST["creditCardInformation_cardType"]; //"MC";
    $reservationInfo['creditCardNumber'] = $_POST["creditCardInformation_cardNumber"]; //"5401999999999999";
    $reservationInfo['creditCardExpirationMonth'] = $_POST["creditCardInformation_expirationDate_month"]; //"01";
    $reservationInfo['creditCardExpirationYear'] = $_POST["creditCardInformation_expirationDate_year"]; //"2013";
    $reservationInfo['creditCardIdentifier'] = $_POST["creditCardInformation_securityCode"]; //"123";

    $book->SetReservationInfo($reservationInfo);

    $addressInfo['address1'] = $_POST["billingAddress_streetAddres_part1"]; //"travelnow";
    $addressInfo['city'] = $_POST["billingAddress_city"]; //"New York";
    $addressInfo['stateProvinceCode'] = $_POST["billingAddress_state"]; //"null";
    $addressInfo['countryCode'] = $_POST["billingAdress_country"]; //"US";
    $addressInfo['postalCode'] = $_POST["billingAddress_zipCode"]; //"12345";

    $book->SetAddressInfo($addressInfo);

    $book->CreateXML_Request();

    //$api->PrintTestDataOnScreen($roomAvail, "");

    $bookR = $book->SendRequest();
    $bookRes = json_decode($bookR);

    $_SESSION['book_email'] = $reservationInfo['email'];
    $_SESSION['book_itid'] = $bookRes->HotelRoomReservationResponse->itineraryId;

    if(!empty($_SESSION["logged_user"]) && $_SESSION["logged_bool"] != false){
        include_once 'itineraryDBclass.php';
        $varIt = new ItineraryDBclass();
        $varIt->connectToDb();
        $userId = $varIt->GetIdUser($_SESSION["logged_user"]);
        $varIt->SetData($userId, $bookRes->HotelRoomReservationResponse->itineraryId);
        $varIt->insertData();
    }
     $url ="confirmationPage.php";
     header("Location:".$url);
}
?>
